/**Filename: blockingQueue.c
 * Author(s): Vladimir Dudnic, Michael Groenier, Derek Fleener
 * Winter 2013 TCSS422
 * Project 2
 */
#include"blockingQueue.h"
#include<stdlib.h>

int blockingQueue_init(blockingQueue* the_blockingQueue){
  int rv = 0;
  pthread_mutex_t* lock;

  lock = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t));
  pthread_mutex_init(lock, NULL);

  the_blockingQueue->l = lock;
  the_blockingQueue->q = (queue*)malloc(sizeof(queue));
  queue_init(the_blockingQueue->q, 1);

  return rv;
}

int blockingQueue_enqueue(blockingQueue* the_blockingQueue, void* item){
  int rv = 0;
  //enter critical region
  pthread_mutex_lock(the_blockingQueue->l);
  rv = queue_enqueue(the_blockingQueue->q, item);
  //exit critical
  pthread_mutex_unlock(the_blockingQueue->l);
  return rv;
}
void* blockingQueue_dequeue(blockingQueue* the_blockingQueue){
  void* item;

  //enter critical region
  pthread_mutex_lock(the_blockingQueue->l);
  item = queue_dequeue(the_blockingQueue->q);
  //exit critical
  pthread_mutex_unlock(the_blockingQueue->l);

  return item;
}

